﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using KinectTD.Input;
namespace KinectTD.UI
{
   public class Button : Control
    {
        protected Texture2D buttonTexture;
        public delegate void ClickedEventHandler(object sender, EventArgs a);
        public event  ClickedEventHandler Clicked;
        private string textureName;
        private float timer = 0;

        protected Color hoverColour;

        public  Color HoverColour
        {
            get { return hoverColour; }
            set { hoverColour = value; }
        }
        

        public Button(string textureName)
            : base()
        {
            this.textureName = textureName;
            hoverColour = new Color(0, 180, 255);
            
        }


        /// <summary>
        /// Updates the button and checks to see if it has been pressed.
        /// </summary>
        /// <param name="gameTime"></param>
        public virtual void Update(GameTime gameTime)
        {
            if (drawRectangle.Contains(Kinect.CursorPosition))
            {
                timer += gameTime.ElapsedGameTime.Milliseconds;

                if (timer >= Engine.CLICK_TIME_OUT)
                {
                    timer = -100;
                    if (Clicked != null)
                    {
                        Clicked(this, null);
                    }
                }
            }
            else
            {
                timer = 0;
            }
        }


        /// <summary>
        /// Draws the button
        /// </summary>
        /// <param name="spriteBatch">The SpriteBatch instance responsible for drawing the button</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(buttonTexture, drawRectangle, colour);
            if (timer > 0 && timer < Engine.CLICK_TIME_OUT)
            {
                float rollHeight = 0;
                rollHeight = ((timer / Engine.CLICK_TIME_OUT) * size.Y);
                spriteBatch.Draw(buttonTexture, new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)rollHeight), hoverColour);
            }
            
        }

        /// <summary>
        /// Loads in the button texture
        /// </summary>
        /// <param name="Content"></param>
        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager Content)
        {
            buttonTexture = Content.Load<Texture2D>(textureName);
           // this.size = new Vector2(buttonTexture.Width, buttonTexture.Height);
        }
    }
}
